package com.klamerek.rainbow.client.charts.geom;

/**
 * A basic line class. 
 *
 */
public class MyLine {

	/*
	 * Coefficients : a * x + b * y = c = 0.
	 */
	private double a;
	private double b;
	private double c;

	/**
	 * Constructor
	 */
	public MyLine(){
		this.a = 0.0;
		this.b = 0.0;
		this.c = 0.0;
	}

	/**
	 * Constructor
	 * @param aPoint
	 * @param bPoint
	 */
	public MyLine (MyPoint aPoint, MyPoint bPoint){
		a = bPoint.getY() - aPoint.getY(); 
		b = aPoint.getX() - bPoint.getX() ;
		c = aPoint.getY() * bPoint.getX() - aPoint.getX() * bPoint.getY() ;
	}

	/**
	 * Constructor
	 * @param a
	 * @param b
	 * @param c
	 */
	public MyLine(double a, double b, double c){
		this.a = a;
		this.b = b;
		this.c = c;
	}

	/**
	 * 
	 * @return
	 */
	public double getA() {
		return a;
	}

	/**
	 * 
	 * @param a
	 */
	public void setA(double a) {
		this.a = a;
	}

	/**
	 * 
	 * @return
	 */
	public double getB() {
		return b;
	}

	/**
	 * 
	 * @param b
	 */
	public void setB(double b) {
		this.b = b;
	}

	/**
	 * 
	 * @return
	 */
	public double getC() {
		return c;
	}

	/**
	 * 
	 * @param c
	 */
	public void setC(double c) {
		this.c = c;
	}

	/**
	 * Get the parallel line.
	 * @param point
	 * @return
	 */
	public MyLine getParallel (MyPoint point){
		double a = this.a; 
		double b = this.b;
		double c = - (this.a * point.getX() + this.b * point.getY());
		MyLine result = new MyLine(a,b,c);
		return result;
	}

	/**
	 * Get perpendicular line.
	 * @param point
	 * @return
	 */
	public MyLine getPerpendicular (MyPoint point){
		double a = - this.b; 
		double b = this.a; 
		double c = this.b * point.getX() - this.a * point.getY();
		MyLine result = new MyLine(a,b,c);
		return result;
	}

	/**
	 * Get the mediator line between aPoint and bPoint.
	 * @param aPoint
	 * @param bPoint
	 * @return
	 */
	public static MyLine getMediator (MyPoint aPoint, MyPoint bPoint){
		double a = bPoint.getX() - aPoint.getX() ; 
		double b = bPoint.getY() - aPoint.getY() ;
		double c = (aPoint.getX() * aPoint.getX() - bPoint.getX() * bPoint.getX() 
				+ aPoint.getY() * aPoint.getY() - bPoint.getY() * bPoint.getY()) * 0.5 ;
		MyLine result = new MyLine(a,b,c);
		return result;
	}

	/**
	 * Return true if the line contains the point.
	 * @param point
	 * @param delta
	 * @return
	 */
	public boolean contains(MyPoint point, double delta){
		boolean result = false;
		if ( Math.abs(this.a * point.getX() + this.b * point.getY() + this.c) <= delta){
			result = true;
		}
		return result;
	}

	/**
	 * Return true if the line contains the point.
	 * @param point
	 * @return
	 */
	public boolean contains(MyPoint point){
		boolean result = this.contains(point, 0.0);
		return result;
	}

}
